#include "MainMenuState.h"

CMainMenuState::CMainMenuState(void)
:m_iCurOption(-1)
{	
	Init();
}

CMainMenuState::~CMainMenuState(void)
{
	for(int i = 0; i < m_Buttons.size(); i++)
	{
		delete m_Buttons[i];
	}
	delete m_sprBackground;
}

void CMainMenuState::Init()
{
	m_Buttons.push_back(new Button("New Game", NEW_GAME, TEX_BTN_NEWGAME, 10.0f, 10.0f, 243.0f, 81.0f));	
	m_Buttons.push_back(new Button("How to play", HOW_TO_PLAY, TEX_HOW_TO_PLAY, 10.0f, 100.0f, 243.0f, 81.0f));
	m_Buttons.push_back(new Button("Options", OPTIONS, TEX_OPTIONS, 10.0f, 200.0f, 243.0f, 81.0f));
	m_Buttons.push_back(new Button("Exit", EXIT, TEX_EXIT, 10.0f, 300.0f, 243.0f, 81.0f));
	m_sprBackground = new CSprite();
	m_sprBackground->SetTexture(TEX_BACKGROUND);
}

void CMainMenuState::Update(float dt)
{		
	// poll input
	DirectInput::GetInstance()->poll();	

	tagPOINT curPos;
	GetCursorPos(&curPos);
	ScreenToClient(gd3dApp->getMainWnd(), &curPos);

	// which button is clicked?
	for(int i = 0; i < m_Buttons.size(); i++)
	{
		Button* btn = m_Buttons[i];
		if(btn->ContainMousePoint(curPos.x, curPos.y))
		{
			btn->Hover(true);
			// if user click on button then switch to other state
			if(DirectInput::GetInstance()->mouseButtonDown(0))
			{
				m_iCurOption = btn->GetId();
			}
		} else
		{
			btn->Hover(false);
		}

		// switch to other state if player click on buttons
		switch(m_iCurOption)
		{
		case NEW_GAME:
			StateManagement::GetInstance()->SwitchState(new CInGameState());
			break;
		case HOW_TO_PLAY:
			StateManagement::GetInstance()->SwitchState(new CHowToPlay());
			break;
		case OPTIONS:
			StateManagement::GetInstance()->SwitchState(new COption());
			break;
		default:
			break;
		}
	}

	if(DirectInput::GetInstance()->keyDown(DIK_RETURN))
	{
		StateManagement::GetInstance()->SwitchState(new CInGameState());
	}
}

void CMainMenuState::Render()
{
	gd3dDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(255, 255, 255), 1.0f, 0);

	RECT formatRect;
	GetClientRect(gd3dApp->getMainWnd(), &formatRect);

	CRect border(0, 0, formatRect.right, formatRect.bottom);
	CRect texCoord(0, 0, 1, 1);
	m_sprBackground->Render(&border, &texCoord);
	/*for(int i = 0; i < m_Buttons.size(); i++) 
	{
		m_Buttons[i]->Render();
	}*/
}

void CMainMenuState::Release()
{

}